package thread_case;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ThreadDemo4 {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue();
        Thread customer = new Thread(()->{
            while (true) {
                try {
                    Integer elem = blockingQueue.take();
                    System.out.println("消费元素:->" + elem);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"消费者");
        Thread producer = new Thread(()->{
            Random random = new Random();
            while (true) {
                Integer elem = random.nextInt(1000);
                System.out.println("生产元素:->" + elem);
                try {
                    blockingQueue.put(elem);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        customer.start();
        producer.start();
    }
}
